package org.mclll.xwangplugin.questTracking.questGoal;

import me.pikamug.quests.events.quester.BukkitQuesterPostUpdateObjectiveEvent;
import me.pikamug.quests.quests.components.BukkitObjective;
import me.pikamug.quests.quests.components.Stage;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.scoreboard.Objective;
import org.jetbrains.annotations.NotNull;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;
import org.mclll.xwangplugin.questTracking.persistence.Persistence;

import java.util.LinkedList;
import java.util.List;

/**
 * @author: geduo
 * @date: 2023/10/12 22:23
 */
public class ReachLocationQuestTrack {
    private static LogUtil log = LoggerFactory.get(GlobalLog.class);
    public static void questTracking(Objective context, Integer line, Stage questsStage) {
        LinkedList<String> locationNames = questsStage.getLocationNames();
        if (locationNames.isEmpty()) {
            return;
        }
        for (String locationName : locationNames) {
            context.getScore(getGoalFormat(locationName)).setScore(line--);
        }
    }

    @NotNull
    public static String getGoalFormat(String locationName) {
        return ChatColor.YELLOW + "前往" + locationName;
    }

    @NotNull
    public static String getFinishFormat(String locationName) {
        return ChatColor.GRAY + "前往" + locationName + "(✓)";
    }


    public static void goalFinish(BukkitQuesterPostUpdateObjectiveEvent event, List<Persistence.SerializeScore> scoreboard) {
        BukkitObjective objective = (BukkitObjective) event.getObjective();
        String locationName = (String) objective.getGoalObject();
        scoreboard.stream()
                .filter(i -> i.getName().equals(getGoalFormat(locationName)))
                .forEach(i -> {
                    i.setName(getFinishFormat(locationName));
                    i.setValue(scoreboard.get(scoreboard.size() - 1).getValue() - 1);
                });
    }


}
